﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ZoneInZone.CApZZZoneInZoneCollisionsManager
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.Collections.Generic;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.ZoneInZone;

public class CApZZZoneInZoneCollisionsManager
{
  private TxColor _collisionColor;
  private TxColor _zoneColor;

  public CApZZZoneInZoneCollisionsManager(TxColor zoneColor)
  {
    this._collisionColor = new TxColor(TxColor.TxColorRed);
    this._collisionColor.Transparency = 0.5;
    this._zoneColor = zoneColor;
  }

  private bool AreZonesInCollision(Zone zone1, Zone zone2)
  {
    bool flag = false;
    if (CApZZZoneInZoneCollisionsManager.CalculateDistance(zone1.Sphere.AbsoluteLocation.Translation, zone2.Sphere.AbsoluteLocation.Translation) < zone1.Radius + zone2.Radius)
      flag = true;
    return flag;
  }

  private static double CalculateDistance(TxVector p1, TxVector p2)
  {
    TxVector txVector = TxVector.op_Subtraction(p1, p2);
    return Math.Sqrt(TxVector.DotProduct(txVector, txVector));
  }

  private void SetOriginalZoneColor(Zone zone) => this.SetSphereColor(zone.Sphere, this._zoneColor);

  private void SetCollisionColor(Zone zone)
  {
    this.SetSphereColor(zone.Sphere, this._collisionColor);
  }

  private void SetSphereColor(TxManipulator sphere, TxColor color)
  {
    if (!sphere.IsValid())
      return;
    sphere.SetColor(color);
  }

  private bool IsZoneInCollision(LinkedListNode<Location> locNode)
  {
    bool flag = false;
    if (locNode.Previous != null && locNode.Previous.Value.Zone != null)
      flag = this.AreZonesInCollision(locNode.Value.Zone, locNode.Previous.Value.Zone);
    if (!flag && locNode.Next != null && locNode.Next.Value.Zone != null)
      flag = this.AreZonesInCollision(locNode.Value.Zone, locNode.Next.Value.Zone);
    return flag;
  }

  internal void ColorCollidingNeighborZones(LinkedList<Location> locations)
  {
    for (LinkedListNode<Location> linkedListNode = locations.First; linkedListNode != null && linkedListNode.Next != null; linkedListNode = linkedListNode.Next)
    {
      if (linkedListNode.Value.Zone != null && linkedListNode.Next.Value.Zone != null && this.AreZonesInCollision(linkedListNode.Value.Zone, linkedListNode.Next.Value.Zone))
      {
        this.SetCollisionColor(linkedListNode.Value.Zone);
        this.SetCollisionColor(linkedListNode.Next.Value.Zone);
      }
    }
  }

  internal void RefreshCollisionColoringArroundNode(LinkedListNode<Location> locNode)
  {
    if (locNode.Previous != null && locNode.Previous.Value.Zone != null)
    {
      if (this.IsZoneInCollision(locNode.Previous))
        this.SetCollisionColor(locNode.Previous.Value.Zone);
      else
        this.SetOriginalZoneColor(locNode.Previous.Value.Zone);
    }
    if (locNode != null && locNode.Value.Zone != null)
    {
      if (this.IsZoneInCollision(locNode))
        this.SetCollisionColor(locNode.Value.Zone);
      else
        this.SetOriginalZoneColor(locNode.Value.Zone);
    }
    if (locNode.Next == null || locNode.Next.Value.Zone == null)
      return;
    if (this.IsZoneInCollision(locNode.Next))
      this.SetCollisionColor(locNode.Next.Value.Zone);
    else
      this.SetOriginalZoneColor(locNode.Next.Value.Zone);
  }
}
